#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include "stack.h"

void stack_create(Stack *pstack)
{
        pstack->increment=50;
        pstack->start_size=50;
        pstack->current=0;
        pstack->size=pstack->start_size;
        pstack->data=(pointer*)malloc(pstack->start_size*sizeof(pointer));
}
void stack_destroy(Stack *pstack)
{
	 pstack->size=0;
	 pstack->current=0;
	 free(pstack->data);
}
void stack_push(Stack *pstack, Pointer value)
{
	if (pstack->size==pstack->current)
	{
		Pointer * buff;
		buff = realloc(pstack->data, ((pstack->size)+(pstack->increment))*sizeof(pointer));
		if (buff != NULL)
			pstack->data = buff;
		else
		{
			free(pstack->data);
			pstack->size = 0;
			pstack->current = 0;
			return;

		}
		pstack->size+=pstack->increment;
	}
		pstack->current++;
        pstack->data[pstack->current-1]=value;
}

size_t stack_size(Stack *pstack)
{
        return pstack->size;
}

pointer stack_pop(Stack *pstack)
{
        if((pstack->current)==0)
        {
               // assert(0);
                return 0;
        }
        pstack->current--;
        return pstack->data[pstack->current];
}

pointer stack_peek(Stack *pstack)
{
        if((pstack->current)==0)
        {
                assert(0);
                return 0;
        }
        return pstack->data[pstack->current-1];
}

void stack_tune(Stack *pstack, size_t initial_size, size_t increment)
{
        pstack->start_size = initial_size;
        pstack->increment = increment;
}
